package com.kchouse.homepage;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kchouse.MainActivity;
import com.kchouse.R;
import com.kchouse.bean.DepositItem;
import com.kchouse.bean.OfferItem;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.ToastUtils;

import java.util.ArrayList;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class DepositmanagementActivity extends BaseActivity implements View.OnClickListener,AdapterView.OnItemClickListener{


    private TextView tv_title;
    private ImageView iv_back;
    private ListView lv_item;
    private LvAdapter adapter;
    private Intent intent;
    private ArrayList<DepositItem> listdeposit=new ArrayList();//定金列表
    private DepositmanagementActivity mContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_depositmanagement);

        mContent = this;
        intiData();
        intiView();
    }

    private void intiView() {

        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("定金管理");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);

        lv_item = (ListView) findViewById(R.id.lv_item);
        adapter = new LvAdapter();
        lv_item.setAdapter(adapter);
        lv_item.setOnItemClickListener(this);

    }

    private void intiData() {
        intent= getIntent();
//        GetDepositmanage();
    }

    @Override
    protected void onResume() {
        super.onResume();
        GetDepositmanage();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String itemid=  listdeposit.get(position).getId();
        intent.putExtra("id",itemid);
        intent.setClass(mContent,DepositDetailsActivity.class);
        startActivity(intent);
    }

    /**
     * 获取定金管理
     */
    private void GetDepositmanage() {
        String url = API.GetDepositmanage+ MainActivity.userid;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数
        params.addHeader("token", MainActivity.token);//添加header信息
        params.addFormDataPart("userId", MainActivity.userid);

        HttpRequest.get(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                listdeposit.clear();
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    JSONObject datajson = JSONObject.parseObject(json.getString("data"));
                    listdeposit.addAll(JSONObject.parseArray(datajson.getString("userEarnestList"), DepositItem.class));
                    adapter.notifyDataSetChanged();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    ToastUtils.getInstanc(mContent).showToast(data.getString("msg"));
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });

    }

    class LvAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return listdeposit.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            DepositItem item = listdeposit.get(position);
            View view = getLayoutInflater().inflate(R.layout.lv_manage_item, null);
            TextView tv_carNumber = (TextView) view.findViewById(R.id.tv_carNumber);
            tv_carNumber.setText(item.getCarNumber());
            TextView tv_MerchantShortName = (TextView) view.findViewById(R.id.tv_MerchantShortName);
            tv_MerchantShortName.setText(item.getMerchantShortName());
            TextView tv_createTime = (TextView) view.findViewById(R.id.tv_createTime);
            tv_createTime.setText(item.getCreateTime().substring(0,10).toString());
            TextView tv_subeva = (TextView) view.findViewById(R.id.tv_subeva);
            if(item.getStatus().equals("YES")){
                tv_subeva.setText("已支付 >");
            }else {
                tv_subeva.setText("未支付 >");
            }
            return view;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
